package com.levtech.moviemanager.db;

import com.levtech.moviemanager.error.DatabaseException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataInitializer extends SQLiteOpenHelper
{

	public DataInitializer(Context context)
	{
		super(context, DataStore.DB_NAME, null, DataStore.DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// create movie table
		db.execSQL("CREATE TABLE IF NOT EXISTS " + DataStore.MOVIE_TABLE +
				"(movie_id INTEGER PRIMARY KEY AUTOINCREMENT," +
				"title TEXT NOT NULL," +
				"description TEXT," +
				"genre TEXT," +
				"format TEXT NOT NULL," +
				"cast TEXT," +
				"studio TEXT);");
		
		// create contact table
		db.execSQL("CREATE TABLE IF NOT EXISTS " + DataStore.CONTACT_TABLE +
				"(contact_id INTEGER PRIMARY KEY AUTOINCREMENT," +
				"firstName TEXT NOT NULL," +
				"lastName TEXT NOT NULL," +
				"phone TEXT);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS " + DataStore.LOAN_TABLE +
				"(loan_id INTEGER PRIMARY KEY AUTOINCREMENT," +
				"movie_id INTEGER NOT NULL," +
				"contact_id INTEGER NOT NULL," +
				"startDate NUMERIC NOT NULL," +
				"endDate NUMERIC NOT NULL," +
				"status TEXT NOT NULL," +
				"FOREIGN KEY(movie_id) REFERENCES " + DataStore.MOVIE_TABLE + "(movie_id)," +
				"FOREIGN KEY(contact_id) REFERENCES " + DataStore.CONTACT_TABLE + "(contact_id));");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		throw new DatabaseException("unsupported operation");
	}
}
